
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>字符串处理 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="数据结构.html" />
    
    
    <link rel="prev" href="基础.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础.html">
            
                <a href="基础.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3" data-path="字符串处理.html">
            
                <a href="字符串处理.html">
            
                    
                    字符串处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="数据结构.html">
            
                <a href="数据结构.html">
            
                    
                    数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="数据结构高级内容.html">
            
                <a href="数据结构高级内容.html">
            
                    
                    数据结构高级内容
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="函数式编程.html">
            
                <a href="函数式编程.html">
            
                    
                    函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="面向对象编程.html">
            
                <a href="面向对象编程.html">
            
                    
                    面向对象编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="类的特殊方法.html">
            
                <a href="类的特殊方法.html">
            
                    
                    类的特殊方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="IO编程.html">
            
                <a href="IO编程.html">
            
                    
                    IO编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="模块.html">
            
                <a href="模块.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="标准库.html">
            
                <a href="标准库.html">
            
                    
                    标准库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="测试.html">
            
                <a href="测试.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="调试.html">
            
                <a href="调试.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >字符串处理</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5B57;&#x7B26;&#x4E32;&#x5904;&#x7406;">&#x5B57;&#x7B26;&#x4E32;&#x5904;&#x7406;</h1>
<hr>
<h2 id="&#x5B57;&#x7B26;&#x4E32;&#x548C;&#x7F16;&#x7801;">&#x5B57;&#x7B26;&#x4E32;&#x548C;&#x7F16;&#x7801;</h2>
<blockquote>
<p>&#x8BA1;&#x7B97;&#x673A;&#x7CFB;&#x7EDF;&#x901A;&#x7528;&#x7684;&#x5B57;&#x7B26;&#x7F16;&#x7801;&#x5DE5;&#x4F5C;&#x65B9;&#x5F0F;&#xFF1A;&#x5728;&#x8BA1;&#x7B97;&#x673A;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x7EDF;&#x4E00;&#x4F7F;&#x7528; Unicode &#x7F16;&#x7801;&#xFF0C;&#x5F53;&#x9700;&#x8981;&#x4FDD;&#x5B58;&#x5230;&#x786C;&#x76D8;&#x6216;&#x8005;&#x9700;&#x8981;&#x4F20;&#x8F93;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x8F6C;&#x6362;&#x4E3A; UTF-8 &#x7F16;&#x7801;&#x3002;<br>UTF-8 &#x7F16;&#x7801;: &#x5B83;&#x628A; Unicode &#x7F16;&#x7801;&#x8F6C;&#x5316;&#x4E3A;&#x201C;&#x53EF;&#x53D8;&#x957F;&#x7F16;&#x7801;&#x201D;&#x3002;&#x5E38;&#x7528;&#x7684;&#x82F1;&#x6587;&#x5B57;&#x6BCD;&#x88AB;&#x7F16;&#x7801;&#x6210; 1 &#x4E2A;&#x5B57;&#x8282;&#xFF0C;&#x6C49;&#x5B57;&#x901A;&#x5E38;&#x662F; 3 &#x4E2A;&#x5B57;&#x8282;&#xFF0C;&#x53EA;&#x6709;&#x5F88;&#x751F;&#x50FB;&#x7684;&#x5B57;&#x7B26;&#x624D;&#x4F1A;&#x88AB;&#x7F16;&#x7801;&#x6210; 4-6&#x4E2A;&#x5B57;&#x8282;&#x3002;&#x5982;&#x679C;&#x4F60;&#x8981;&#x4F20;&#x8F93;&#x7684;&#x6587;&#x672C;&#x5305;&#x542B;&#x5927;&#x91CF;&#x82F1;&#x6587;&#x5B57;&#x7B26;&#xFF0C;&#x7528; UTF-8 &#x7F16;&#x7801;&#x5C31;&#x80FD;&#x8282;&#x7701;&#x7A7A;&#x95F4;&#x3002;  </p>
</blockquote>
<p>&#x5728;Python 3 &#x7248;&#x672C;&#x4E2D;&#xFF0C;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x4EE5; Unicode &#x7F16;&#x7801;&#x7684;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;Python &#x7684;&#x5B57;&#x7B26;&#x4E32;&#x652F;&#x6301;&#x591A;&#x8BED;&#x8A00;&#x3002;<br>&#x7531;&#x4E8E; Python &#x6E90;&#x4EE3;&#x7801;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x6587;&#x672C;&#x6587;&#x4EF6;&#xFF0C;&#x6240;&#x4EE5;&#xFF0C;&#x5F53;&#x4F60;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#x4E2D;&#x5305;&#x542B;&#x4E2D;&#x6587;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5728;&#x4FDD;&#x5B58;&#x6E90;&#x4EE3;&#x7801;&#x65F6;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x52A1;&#x5FC5;&#x6307;&#x5B9A;&#x4FDD;&#x5B58;&#x4E3A; UTF-8 &#x7F16;&#x7801;&#x3002;&#x5F53;Python &#x89E3;&#x91CA;&#x5668;&#x8BFB;&#x53D6;&#x6E90;&#x4EE3;&#x7801;&#x65F6;&#xFF0C;&#x4E3A;&#x4E86;&#x8BA9;&#x5B83;&#x6309; UTF-8 &#x7F16;&#x7801;&#x8BFB;&#x53D6;&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x5E38;&#x5728;&#x6587;&#x4EF6;&#x5F00;&#x5934;&#x5199;&#x4E0A;&#x8FD9;&#x4E24;&#x884C;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-comment">#!/usr/bin/env python3</span>
<span class="hljs-comment"># -*- coding: utf-8 -*-</span>
</code></pre>
<p>&#x7B2C;&#x4E00;&#x884C;&#x544A;&#x8BC9;Linux&#x7CFB;&#x7EDF;&#x5E94;&#x8BE5;&#x8FD0;&#x884C;&#x54EA;&#x4E2A;&#x89E3;&#x91CA;&#x5668;&#x3002;<br>&#x7B2C;&#x4E8C;&#x884C;&#x544A;&#x8BC9; Python &#x89E3;&#x91CA;&#x5668;&#xFF0C;&#x6309;&#x7167; UTF-8 &#x7F16;&#x7801;&#x8BFB;&#x53D6;&#x6E90;&#x4EE3;&#x7801;&#xFF0C;&#x5426;&#x5219;&#xFF0C;&#x4F60;&#x5728;&#x6E90;&#x4EE3;&#x7801;&#x4E2D;&#x5199;&#x7684;&#x4E2D;&#x6587;&#x8F93;&#x51FA;&#x53EF;&#x80FD;&#x4F1A;&#x6709;&#x4E71;&#x7801;&#x3002;  </p>
<ol>
<li><p>&#x4EE5; Unicode &#x8868;&#x793A;&#x7684; str &#x901A;&#x8FC7; <code>encode()</code>&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x7F16;&#x7801;&#x4E3A;&#x6307;&#x5B9A;&#x7684; bytes&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">&apos;ABC&apos;</span>.encode(<span class="hljs-string">&apos;ascii&apos;</span>)
<span class="hljs-string">b&apos;ABC&apos;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">&apos;ABC&apos;</span>.encode(<span class="hljs-string">&apos;utf-8&apos;</span>)
<span class="hljs-string">b&apos;ABC&apos;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">&apos;&#x4E2D;&#x6587;&apos;</span>.encode(<span class="hljs-string">&apos;utf-8&apos;</span>)
<span class="hljs-string">b&apos;\xe4\xb8\xad\xe6\x96\x87&apos;</span>
</code></pre>
<p>&#x5728; bytes &#x4E2D;&#xFF0C;&#x65E0;&#x6CD5;&#x663E;&#x793A;&#x4E3A; ASCII &#x5B57;&#x7B26;&#x7684;&#x5B57;&#x8282;&#xFF0C;&#x7528;<code>\x##</code>&#x663E;&#x793A;&#x3002;<br>&#x7EAF;&#x82F1;&#x6587;&#x7684; str &#x53EF;&#x4EE5;&#x7528; ASCII&#x6216;UTF-8 &#x7F16;&#x7801;&#x4E3A; bytes&#xFF0C;&#x5185;&#x5BB9;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x542B;&#x6709;&#x4E2D;&#x6587;&#x7684; str&#x53EF;&#x4EE5;&#x7528; UTF-8 &#x7F16;&#x7801;&#x4E3A; bytes&#x3002;<br>&#x542B;&#x6709;&#x4E2D;&#x6587;&#x7684; str &#x65E0;&#x6CD5;&#x7528; ASCII &#x7F16;&#x7801;&#xFF0C;&#x56E0;&#x4E3A;&#x4E2D;&#x6587;&#x7F16;&#x7801;&#x7684;&#x8303;&#x56F4;&#x8D85;&#x8FC7;&#x4E86;ASCII&#x7F16;&#x7801;&#x7684;&#x8303;&#x56F4;&#xFF0C; Python &#x4F1A;&#x62A5;&#x9519;&#x3002; </p>
</li>
<li><p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x4ECE;&#x7F51;&#x7EDC;&#x6216;&#x78C1;&#x76D8;&#x4E0A;&#x8BFB;&#x53D6;&#x4E86;&#x5B57;&#x8282;&#x6D41;&#xFF0C;&#x90A3;&#x4E48;&#x8BFB;&#x5230;&#x7684;&#x6570;&#x636E;&#x5C31;&#x662F;bytes&#x3002;&#x8981;&#x628A; bytes &#x53D8;&#x4E3A; str&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x7528; <code>decode()</code>&#x65B9;&#x6CD5;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">b&apos;ABC&apos;</span>.decode(<span class="hljs-string">&apos;ascii&apos;</span>)
<span class="hljs-string">&apos;ABC&apos;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">b&apos;\xe4\xb8\xad\xe6\x96\x87&apos;</span>.decode(<span class="hljs-string">&apos;utf-8&apos;</span>)
<span class="hljs-string">&apos;&#x4E2D;&#x6587;&apos;</span>
</code></pre>
</li>
<li><p>&#x8981;&#x8BA1;&#x7B97; str &#x5305;&#x542B;&#x591A;&#x5C11;&#x4E2A;&#x5B57;&#x7B26;&#xFF0C;&#x53EF;&#x4EE5;&#x7528; <code>len()</code>&#x51FD;&#x6570;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>len(<span class="hljs-string">&apos;ABC&apos;</span>)
<span class="hljs-number">3</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>len(<span class="hljs-string">&apos;&#x4E2D;&#x6587;&apos;</span>)
<span class="hljs-number">2</span>
</code></pre>
<p><code>len()</code>&#x51FD;&#x6570;&#x8BA1;&#x7B97;&#x7684;&#x662F; str &#x7684;&#x5B57;&#x7B26;&#x6570;&#xFF0C;&#x5982;&#x679C;&#x6362;&#x6210; bytes&#xFF0C; <code>len()</code>&#x51FD;&#x6570;&#x5C31;&#x8BA1;&#x7B97;&#x5B57;&#x8282;&#x6570;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>len(<span class="hljs-string">b&apos;ABC&apos;</span>)
<span class="hljs-number">3</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>len(<span class="hljs-string">b&apos;\xe4\xb8\xad\xe6\x96\x87&apos;</span>)
<span class="hljs-number">6</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>len(<span class="hljs-string">&apos;&#x4E2D;&#x6587;&apos;</span>.encode(<span class="hljs-string">&apos;utf-8&apos;</span>))
<span class="hljs-number">6</span>
</code></pre>
</li>
<li><p>&#x5728;&#x64CD;&#x4F5C;&#x5B57;&#x7B26;&#x4E32;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x7ECF;&#x5E38;&#x9047;&#x5230; str &#x548C; bytes &#x7684;&#x4E92;&#x76F8;&#x8F6C;&#x6362;&#x3002;&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x4E71;&#x7801;&#x95EE;&#x9898;&#xFF0C;&#x5E94;&#x5F53;&#x59CB;&#x7EC8;&#x575A;&#x6301;&#x4F7F;&#x7528; UTF-8 &#x7F16;&#x7801;&#x5BF9; str &#x548C; bytes &#x8FDB;&#x884C;&#x8F6C;&#x6362;&#x3002;</p>
</li>
</ol>
<h2 id="&#x683C;&#x5F0F;&#x5316;">&#x683C;&#x5F0F;&#x5316;</h2>
<h3 id="&#x65E7;&#x5F0F;&#x98CE;&#x683C;">&#x65E7;&#x5F0F;&#x98CE;&#x683C;</h3>
<p>&#x65E7;&#x7684;&#x683C;&#x5F0F;&#x5316;&#x65B9;&#x5F0F;&#x548C; C &#x8BED;&#x8A00;&#x662F;&#x4E00;&#x81F4;&#x7684;&#xFF0C;&#x7528;%&#x5B9E;&#x73B0;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">&apos;Hello %s&apos;</span> % <span class="hljs-string">&apos;world&apos;</span>
<span class="hljs-string">&apos;Hello world&apos;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">&apos;Hi, %s, you have $%d.&apos;</span> % (<span class="hljs-string">&apos;Michael&apos;</span>, <span class="hljs-number">1000000</span>)
<span class="hljs-string">&apos;Hi, Michael, you have $1000000.&apos;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x53EA;&#x6709;&#x4E00;&#x4E2A;%&#x53C2;&#x6570;&#xFF0C;&#x62EC;&#x53F7;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x3002;<br>&#x4F7F;&#x7528;%s&#x65F6;&#xFF0C;&#x5B83;&#x4F1A;&#x628A;&#x4EFB;&#x4F55;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<h3 id="&#x65B0;&#x5F0F;&#x98CE;&#x683C;">&#x65B0;&#x5F0F;&#x98CE;&#x683C;</h3>
<p>&#x65B0;&#x7684;&#x683C;&#x5F0F;&#x5316;&#x65B9;&#x5F0F;&#x662F;&#x4F7F;&#x7528;<code>format()</code>&#x65B9;&#x6CD5;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">&apos;{} is {}&apos;</span>.format(<span class="hljs-string">&apos;erick&apos;</span>, <span class="hljs-number">100</span>)
<span class="hljs-string">&apos;erick is 100&apos;</span>
</code></pre>
<blockquote>
<p>&#x5B98;&#x65B9;&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x65B0;&#x5F0F;&#x98CE;&#x683C;&#x8FDB;&#x884C;&#x683C;&#x5F0F;&#x5316;&#x3002;</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="基础.html" class="navigation navigation-prev " aria-label="Previous page: 基础">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="数据结构.html" class="navigation navigation-next " aria-label="Next page: 数据结构">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"字符串处理","level":"1.3","depth":1,"next":{"title":"数据结构","level":"1.4","depth":1,"path":"数据结构.md","ref":"数据结构.md","articles":[]},"previous":{"title":"基础","level":"1.2","depth":1,"path":"基础.md","ref":"基础.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"字符串处理.md","mtime":"2017-08-22T15:18:36.906Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-08-27T04:11:18.522Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

